package com.nanjia.boot.product.sku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.product.sku.converter.ProductSkuConverter;
import com.nanjia.boot.product.sku.mapper.ProductSkuMapper;
import com.nanjia.boot.product.sku.model.entity.ProductSku;
import com.nanjia.boot.product.sku.model.form.ProductSkuForm;
import com.nanjia.boot.product.sku.model.query.ProductSkuQuery;
import com.nanjia.boot.product.sku.model.vo.ProductSkuVO;
import com.nanjia.boot.product.sku.service.ProductSkuService;
import com.nanjia.boot.product.sku.spec.value.mapper.SkuSpecValueMapper;
import com.nanjia.boot.product.sku.spec.value.model.entity.SkuSpecValue;
import com.nanjia.boot.product.spec.value.mapper.SpecValueMapper;
import com.nanjia.boot.product.spec.value.model.entity.SpecValue;
import com.nanjia.boot.product.spu.mapper.ProductSpuMapper;
import com.nanjia.boot.product.spu.model.entity.ProductSpu;
import com.nanjia.boot.warehouse.service.WarehouseService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品库存单元(SKU)服务实现类
 *
 * @author 周洋
 * @since 2025-07-10 14:15
 */
@Service
@RequiredArgsConstructor
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements ProductSkuService {

    private final ProductSkuConverter productSkuConverter;

    private final ProductSkuMapper productSkuMapper;

    private final WarehouseService warehouseService;

    private final SkuSpecValueMapper skuSpecValueMapper;

    private final SpecValueMapper specValueMapper;

    private final ProductSpuMapper productSpuMapper;

    /**
     * 获取商品库存单元(SKU)分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<ProductSkuVO>} 商品库存单元(SKU)分页列表
     */
    @Override
    public IPage<ProductSkuVO> getProductSkuPage(ProductSkuQuery queryParams) {
        Page<ProductSkuVO> pageVO = this.baseMapper.getProductSkuPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取商品库存单元(SKU)表单数据
     *
     * @param id 商品库存单元(SKU)ID
     * @return 商品库存单元(SKU)表单数据
     */
    @Override
    public ProductSkuForm getProductSkuFormData(Long id) {
        ProductSku entity = this.getById(id);
        ProductSpu productSpu = productSpuMapper.selectById(entity.getSpuId());
        ProductSkuForm form = productSkuConverter.toForm(entity);
        form.setProductName(productSpu.getSpuName());

        List<SkuSpecValue> skuSpecValues = skuSpecValueMapper.selectList(new LambdaQueryWrapper<SkuSpecValue>().eq(SkuSpecValue::getSkuId, id).orderByDesc(SkuSpecValue::getId));
        if (CollectionUtils.isNotEmpty(skuSpecValues)) {
            List<Long> specValueIds = skuSpecValues.stream().map(SkuSpecValue::getSpecValueId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(specValueIds)) {
                form.setSpecValueIds(String.join(";",specValueIds.stream().map(String::valueOf).collect(Collectors.toList())));
                List<SpecValue> specValues = specValueMapper.selectBatchIds(specValueIds);
                if (CollectionUtils.isNotEmpty(specValues)) {
                    form.setSpecValueNames(String.join(";",specValues.stream().map(SpecValue::getValueName).collect(Collectors.toList())));
                }
            }
        }
        form.setCostPrice(new BigDecimal("0.00"));
        return form;
    }
}
